cmake_minimum_required (VERSION 2.8)
cmake_policy(VERSION 2.8)
project(libonion)

set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)

# Some tweak parameters
SET(ONION_USE_SSL false CACHE BOOL "Use SSL, requires GnuTLS")
SET(ONION_USE_PAM false CACHE BOOL "Compile PAM handler. Needs libpam")
SET(ONION_USE_PTHREADS true CACHE BOOL "Compile with pthreads support. Needed for O_THREAD and O_POOL modes")
SET(ONION_USE_PNG false CACHE BOOL "Adds support for simple image handler")
SET(ONION_USE_JPEG false CACHE BOOL "Adds support for simple image handler")
SET(ONION_USE_XML2 false CACHE BOOL "Adds support for XML2 lib, which is needed for WebDAV handler")
SET(ONION_USE_SYSTEMD false CACHE BOOL "Adds simple support for systemd")
SET(ONION_USE_SQLITE3 false CACHE BOOL "Adds support for sqlite3 sessions")
SET(ONION_USE_GC false CACHE BOOL "Compile Boehm GC examples")
SET(ONION_USE_TESTS false CACHE BOOL "Compile the tests")
SET(ONION_EXAMPLES false CACHE BOOL "Compile the examples")
SET(ONION_USE_BINDINGS_CPP false CACHE BOOL "Compile the CPP bindings")
SET(ONION_POLLER default CACHE string "Default poller to use: default | epoll | libev | libevent")
########

execute_process(
	COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/git-version-gen" "${CMAKE_CURRENT_SOURCE_DIR}"
	OUTPUT_VARIABLE ONION_VERSION)

message(STATUS "Onion version is ${ONION_VERSION}")


if (${CMAKE_SYSTEM_NAME} STREQUAL "FreeBSD")
  SET(ONION_USE_PAM false)
endif (${CMAKE_SYSTEM_NAME} STREQUAL "FreeBSD")

SET(LIBPATH /usr/lib /usr/local/lib)

IF (NOT DEFINED CMAKE_INSTALL_BINDIR)
SET(CMAKE_INSTALL_BINDIR bin)
ENDIF (NOT DEFINED CMAKE_INSTALL_BINDIR)

IF (NOT DEFINED CMAKE_INSTALL_LIBDIR)
SET(CMAKE_INSTALL_LIBDIR lib)
ENDIF (NOT DEFINED CMAKE_INSTALL_LIBDIR)

IF (NOT DEFINED INCLUDE_PATH)
SET(INCLUDE_PATH /usr/include)
ENDIF (NOT DEFINED INCLUDE_PATH)


IF (NOT DEFINED CMAKE_INSTALL_INCLUDEDIR)
SET(CMAKE_INSTALL_INCLUDEDIR include/onion)
ENDIF(NOT DEFINED CMAKE_INSTALL_INCLUDEDIR)

if (${ONION_POLLER} STREQUAL "default")
	if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
		set (ONION_POLLER "epoll")
	else(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
		set (ONION_POLLER "libev")
	endif(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
endif (${ONION_POLLER} STREQUAL "default")

message(STATUS "Using ${ONION_POLLER} as poller")


if (${ONION_USE_SSL})
	find_library(GNUTLS_LIB NAMES gnutls PATH ${LIBPATH})
	find_library(GCRYPT_LIB NAMES gcrypt PATH ${LIBPATH})
	if (GNUTLS_LIB AND GCRYPT_LIB)
		set(GNUTLS_ENABLED true)
		message(STATUS "Gnutls found. SSL support is compiled in.")
	else(GNUTLS_LIB AND GCRYPT_LIB)
		message("Gnutls or gcrypt not found. SSL support is not compiled in.")
	endif(GNUTLS_LIB AND GCRYPT_LIB)
endif(${ONION_USE_SSL})

if (${ONION_USE_SQLITE3})
	find_library(SQLITE3_LIB NAMES sqlite3 PATH ${LIBPATH})
	if (SQLITE3_LIB)
		set(SQLITE3_ENABLED true)
		message(STATUS "SQLite found. Sqlite session support is compiled in.")
	else(SQLITE3_LIB)
		message("SQLite not found. Sqlite session support is not compiled in.")
	endif(SQLITE3_LIB)
endif(${ONION_USE_SQLITE3})

if (${ONION_USE_PTHREADS})
	find_library(PTHREADS_LIB NAMES pthread PATH ${LIBPATH})
	if(PTHREADS_LIB)
		message(STATUS "pthreads found. Threading support is compiled in.")
		set(PTHREADS true)
	else(PTHREADS_LIB)
		message("pthreads not found. Threading is not supported.")
	endif(PTHREADS_LIB)
endif(${ONION_USE_PTHREADS})

if (${ONION_USE_XML2})
	find_library(XML2_LIB NAMES xml2 PATH ${LIBPATH})
  find_library(Z_LIB NAMES z PATH ${LIBPATH})
	find_path(XML2_HEADERS NAMES libxml/xmlwriter.h HINTS ${INCLUDE_PATH} PATH_SUFFIXES libxml2)
	if(XML2_HEADERS)
		message(STATUS "libxml2 found. WebDAV support is compiled in.")
		set(XML2_ENABLED true)
	else(XML2_HEADERS)
		message("libxml2 not found. WebDAV is not supported. ${XML2_HEADERS} ${XML2_LIB}")
	endif(XML2_HEADERS)
endif(${ONION_USE_XML2})

if (${ONION_USE_PAM})
	find_library(PAM_LIB NAMES pam pam_misc PATH ${LIBPATH})
	if (PAM_LIB)
		set(PAM_ENABLED true)
		message(STATUS "pam found. PAM support is compiled in in handlers.")
	else (PAM_LIB)
		message("pam not found. No PAM support.")
  endif (PAM_LIB)
endif (${ONION_USE_PAM})

if (${ONION_USE_PNG})
	find_library(PNG_LIB NAMES png PATH ${LIBPATH})
	if (PNG_LIB)
		set(PNG_ENABLED true)
		set(PNG_LIB ${PNG_LIB})
		message(STATUS "libpng found. png support is compiled in at extras.")
		find_library(CAIRO_LIB NAMES cairo PATH ${LIBPATH})
		find_file(CAIRO_HEADER cairo/cairo.h ${INCLUDE_PATH})
		if (CAIRO_HEADER)
			set(CAIRO_ENABLED true)
			set(CAIRO_LIB ${CAIRO_LIB})
			message(STATUS "libcairo found. Adding cairo examples. ${CAIRO_LIB}")
		else (CAIRO_HEADER)
			message(STATUS "libcairo NOT found.")
		endif (CAIRO_HEADER)
	else (PNG_LIB)
		message("libpng not found. No png support.")
  endif (PNG_LIB)
endif (${ONION_USE_PNG})

if (${ONION_USE_JPEG})
	find_library(JPEG_LIB NAMES jpeg PATH ${LIBPATH})
	if (JPEG_LIB)
		set(JPEG_ENABLED true)
		set(JPEG_LIB ${JPEG_LIB})
		message(STATUS "libjpeg found. jpeg support is compiled in at extras.")
	else (JPEG_LIB)
		message("libjpeg not found. No jpeg support.")
  endif (JPEG_LIB)
endif (${ONION_USE_JPEG})

if (${ONION_USE_GC})
	find_library(GC_LIB NAMES gc PATH ${LIBPATH})
	find_file(GC_HEADER gc.h ${INCLUDE_PATH})
	if (GC_LIB AND GC_HEADER)
		message(STATUS "libgc found, compiling Boehm GC examples")
		set(GC_ENABLED true)
	else (GC_LIB AND GC_HEADER)
		message(STATUS "libgc not found, NOT compiling Boehm GC examples")
	endif (GC_LIB AND GC_HEADER)
endif (${ONION_USE_GC})


find_library(CURL_LIB NAMES curl PATH ${LIBPATH})
if(CURL_LIB)
	message(STATUS "curl found. Some extra test are compiled.")
	set(CURL true)
else(CURL_LIB)
	message("curl not found. Some examples wil not be built.")
endif(CURL_LIB)

if (${ONION_USE_SYSTEMD})
	find_library(SD_DAEMON_LIB NAMES systemd-daemon PATH ${LIBPATH})
	if (SD_DAEMON_LIB)
		set(SYSTEMD_ENABLED true)
		message(STATUS "libsystemd-daemon found. Systemd support compiled in.")
	else(SD_DAEMON_LIB)
		message("libsystemd-daemon not found. Systemd support is not compiled in. Install libsystemd-dev | systemd-devel")
	endif(SD_DAEMON_LIB)
endif(${ONION_USE_SYSTEMD})


# defines
if (GNUTLS_ENABLED)
	add_definitions(-DHAVE_GNUTLS)
endif(GNUTLS_ENABLED)
if (PTHREADS)
	add_definitions(-DHAVE_PTHREADS)
endif(PTHREADS)
if (PAM_ENABLED)
	add_definitions(-DHAVE_PAM)
endif(PAM_ENABLED)
if (XML2_ENABLED)
	add_definitions(-DHAVE_WEBDAV)
endif(XML2_ENABLED)
if (SYSTEMD_ENABLED)
	add_definitions(-DHAVE_SYSTEMD)
endif (SYSTEMD_ENABLED)
if (SQLITE3_ENABLED)
	add_definitions(-DHAVE_SQLITE3)
endif (SQLITE3_ENABLED)
add_definitions(-D_BSD_SOURCE)
add_definitions(-D_DEFAULT_SOURCE)
add_definitions(-D_POSIX_C_SOURCE=200112L)
if(PNG_ENABLED)
	add_definitions(-DPNG_ENABLED)
endif(PNG_ENABLED)
if(JPEG_ENABLED)
	add_definitions(-DJPEG_ENABLED)
endif(JPEG_ENABLED)


add_definitions(-DONION_VERSION="${ONION_VERSION}")


##
IF (${CMAKE_BUILD_TYPE} MATCHES "Debug")
	add_definitions(-D__DEBUG__)
	SET(CMAKE_C_FLAGS "-Wall -O0 -fPIC ${CMAKE_C_FLAGS}")
	SET(CMAKE_CXX_FLAGS "-Wall -O0 -fPIC ${CMAKE_CXX_FLAGS}")
ELSE (${CMAKE_BUILD_TYPE} MATCHES "Debug")
	SET(CMAKE_C_FLAGS "-Wall -O1 -fPIC ${CMAKE_C_FLAGS}")
	SET(CMAKE_CXX_FLAGS "-Wall -O1 -fPIC ${CMAKE_CXX_FLAGS}")
ENDIF (${CMAKE_BUILD_TYPE} MATCHES "Debug")


project (onion)

INCLUDE(CPackConfig.cmake)
INCLUDE(CPack)

add_subdirectory(src)
add_subdirectory(tools)

SET(OTEMPLATE otemplate)
SET(OPACK opack)

if (${ONION_EXAMPLES})
	add_subdirectory(examples)
endif (${ONION_EXAMPLES})

if (${ONION_USE_TESTS})
	enable_testing()
endif(${ONION_USE_TESTS})

#add_subdirectory(tests)
